{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 百度AI开放平台- OCR(光学字符识别)\n",
    "# 学生实践周\n",
    "\n",
    "## 通用文字识别\n",
    "\n",
    "* 接口描述\n",
    "> 在通用文字识别的基础上，提供更高精度的识别服务，支持更多语种识别（丹麦语、荷兰语、马来语、瑞典语、印尼语、波兰语、罗马尼亚语、土耳其语、希腊语、匈牙利语、泰语、越语、阿拉伯语、印地语及部分中国少数民族语言），并将字库从1w+扩展到2w+，能识别所有常用字和大部分生僻字。\n",
    "* 请求示例\n",
    "> 1. HTTP 方法：POST\n",
    "> 1. 请求URL： https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic\n",
    "\n",
    "* 请求参数\n",
    "> URL参数：access_token\t\n",
    "> Header : Content-Type\t\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 示例代码1\n",
    "\n",
    "```\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "通用文字识别（高精度版）\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('[本地文件]', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = '[调用鉴权接口获取的token]'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 示例代码2\n",
    "\n",
    "```\n",
    "# encoding:utf-8\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "通用物体和场景识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general\"\n",
    "\n",
    "# 二进制方式打开图片文件\n",
    "# 1.图片文件准备\n",
    "f = open('xihu.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "# 2. 酬载准备\n",
    "payload={\n",
    "    'access_token':zhichao_AT,\n",
    "    'image':img,\n",
    "    'baike_num':5\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 实践A-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import base64"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.01a9f3fc04780e982f6b8f2ec23c9f72.2592000.1652359603.282335-25954484'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=GWpBsEK5GQZsN5rqDS3AtNgI&client_secret=N2avMUxoXHLbwGcms23V7pPF45eUTjzl'\n",
    "response = requests.get(host)\n",
    "if response:\n",
    "#     display(response.json())\n",
    "    display(response.json()[ 'access_token'])\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'words_result': [{'words': '时光未至'},\n",
       "  {'words': '我们要学会在生活的这端'},\n",
       "  {'words': '去眺望和等待'},\n",
       "  {'words': '能让我们幸福的另一个人'}],\n",
       " 'words_result_num': 4,\n",
       " 'log_id': 1513859076478141988}"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "通用文字识别（高精度版）\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('F:/大三上-API/img/img1.png', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = '24.76e30f9165844c9d19d3480d1f74a5ff.2592000.1652357925.282335-25954484'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    display(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "#words_result:识别结果数组\n",
    "#words_result_num:识别结果数，表示words_result的元素个数\n",
    "#log_id:唯一的log id，用于问题定位"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 网络图片文字识别"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 实践A-2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'words_result': [{'words': '我前生是一条鱼'},\n",
       "  {'words': '我只栖息在'},\n",
       "  {'words': '你掌心里的海'}],\n",
       " 'words_result_num': 3,\n",
       " 'log_id': 1513859984502378589}"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "通用文字识别（高精度版）\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('F:/大三上-API/img/img2.png', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = '24.76e30f9165844c9d19d3480d1f74a5ff.2592000.1652357925.282335-25954484'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    display(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 手写文字识别"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 实践A-3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'words_result': [{'location': {'top': 63,\n",
       "    'left': 102,\n",
       "    'width': 619,\n",
       "    'height': 208},\n",
       "   'words': '陪你看过最长的电影遇过很刻'},\n",
       "  {'location': {'top': 124, 'left': 31, 'width': 696, 'height': 226},\n",
       "   'words': '次下雨天,可是我们还是没在一起'},\n",
       "  {'location': {'top': 171, 'left': 43, 'width': 741, 'height': 241},\n",
       "   'words': '原来,电影票上的字会随时间消失'},\n",
       "  {'location': {'top': 231, 'left': 59, 'width': 745, 'height': 248},\n",
       "   'words': '不见就像当初陪你看电影的人。'},\n",
       "  {'location': {'top': 367, 'left': 632, 'width': 154, 'height': 87},\n",
       "   'words': '烟雨容'}],\n",
       " 'words_result_num': 5,\n",
       " 'log_id': 1513860014163995515}"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "手写文字识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('F:/大三上-API/img/img3.png', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = '24.76e30f9165844c9d19d3480d1f74a5ff.2592000.1652357925.282335-25954484'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "     display(response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 二维码识别"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[草料二维码生成器](https://cli.im/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 实践A-4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'codes_result': [{'text': ['我是潘丽璇'], 'type': 'QR_CODE'}],\n",
       " 'codes_result_num': 1,\n",
       " 'log_id': 1513860907242077774}"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "二维码识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/qrcode\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('F:/大三上-API/img/img4.png', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = '24.76e30f9165844c9d19d3480d1f74a5ff.2592000.1652357925.282335-25954484'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    display (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 卡片类文字识别"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 实践A-5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'words_result': {'姓名': {'location': {'top': 86,\n",
       "    'left': 144,\n",
       "    'width': 75,\n",
       "    'height': 30},\n",
       "   'words': '林锦清'},\n",
       "  '民族': {'location': {'top': 138, 'left': 264, 'width': 20, 'height': 24},\n",
       "   'words': '汉'},\n",
       "  '住址': {'location': {'top': 231, 'left': 137, 'width': 255, 'height': 60},\n",
       "   'words': '广西桂平市社坡镇太安村细村屯40号'},\n",
       "  '公民身份号码': {'location': {'top': 353, 'left': 240, 'width': 326, 'height': 29},\n",
       "   'words': '450881198608123212'},\n",
       "  '出生': {'location': {'top': 182, 'left': 141, 'width': 199, 'height': 26},\n",
       "   'words': '19860812'},\n",
       "  '性别': {'location': {'top': 138, 'left': 142, 'width': 22, 'height': 25},\n",
       "   'words': '男'}},\n",
       " 'words_result_num': 6,\n",
       " 'idcard_number_type': 1,\n",
       " 'image_status': 'normal',\n",
       " 'log_id': 1513860788864039262}"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "身份证识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/idcard\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('F:/大三上-API/img/img5.png', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"id_card_side\":\"front\",\"image\":img}\n",
    "access_token = '24.76e30f9165844c9d19d3480d1f74a5ff.2592000.1652357925.282335-25954484'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    display (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 财务类文字识别-增值税发票识别"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 实践A-2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'words_result': [{'words': '43001952112'},\n",
       "  {'words': '广东增通发票'},\n",
       "  {'words': 'N№65252512'},\n",
       "  {'words': '43001952112'},\n",
       "  {'words': '东'},\n",
       "  {'words': '65252512'},\n",
       "  {'words': '开票日期：2020年02月08日'},\n",
       "  {'words': '购'},\n",
       "  {'words': '名'},\n",
       "  {'words': '称：深圳精一日用机械有限公司'},\n",
       "  {'words': '密'},\n",
       "  {'words': '17/1=/'},\n",
       "  {'words': '(9<659/7+7/'},\n",
       "  {'words': '25'},\n",
       "  {'words': '买'},\n",
       "  {'words': '纳税人识别号：91440300760188888E'},\n",
       "  {'words': '码'},\n",
       "  {'words': '171'},\n",
       "  {'words': '(9'},\n",
       "  {'words': '659'},\n",
       "  {'words': '方'},\n",
       "  {'words': '地址、电话：深圳市南山区粤海街道金蝶软件园0755-83064882'},\n",
       "  {'words': '4<90898=24427'},\n",
       "  {'words': '==《》726《42=47'},\n",
       "  {'words': '开户行及账号：高新园招商支行13266446562698500'},\n",
       "  {'words': '区'},\n",
       "  {'words': '货物或应税劳务、服务名称'},\n",
       "  {'words': '规格型号'},\n",
       "  {'words': '单位'},\n",
       "  {'words': '数量'},\n",
       "  {'words': '单价'},\n",
       "  {'words': '金额'},\n",
       "  {'words': '税率'},\n",
       "  {'words': '税颖'},\n",
       "  {'words': '墙面挂钩螺丝钉'},\n",
       "  {'words': '100*100'},\n",
       "  {'words': '千克'},\n",
       "  {'words': '778'},\n",
       "  {'words': '884.97'},\n",
       "  {'words': '327.66'},\n",
       "  {'words': '77%'},\n",
       "  {'words': '327.66'},\n",
       "  {'words': '不锈钢管'},\n",
       "  {'words': '30*30'},\n",
       "  {'words': '元'},\n",
       "  {'words': '122'},\n",
       "  {'words': '417.79'},\n",
       "  {'words': '854.60'},\n",
       "  {'words': '7%'},\n",
       "  {'words': '854.60'},\n",
       "  {'words': '第二联:发票联'},\n",
       "  {'words': '防盗指纹门锁'},\n",
       "  {'words': '80*80'},\n",
       "  {'words': '千元'},\n",
       "  {'words': '357'},\n",
       "  {'words': '582.03'},\n",
       "  {'words': '548.72'},\n",
       "  {'words': '9%'},\n",
       "  {'words': '548.72'},\n",
       "  {'words': '树脂切割片'},\n",
       "  {'words': '10*10'},\n",
       "  {'words': '吨'},\n",
       "  {'words': '231'},\n",
       "  {'words': '638.77'},\n",
       "  {'words': '752.87'},\n",
       "  {'words': '87%'},\n",
       "  {'words': '752.87'},\n",
       "  {'words': '合'},\n",
       "  {'words': '计'},\n",
       "  {'words': '￥120000.00'},\n",
       "  {'words': '￥15600.00'},\n",
       "  {'words': '购买方记账凭证'},\n",
       "  {'words': '价税合计（大写）'},\n",
       "  {'words': '壹拾参万伍仟陆百圆整'},\n",
       "  {'words': '(小写)'},\n",
       "  {'words': '￥16542.00'},\n",
       "  {'words': '名'},\n",
       "  {'words': '称：深圳市宝安宜家塑料有限公司'},\n",
       "  {'words': '备'},\n",
       "  {'words': '青龙偃月刀'},\n",
       "  {'words': '销售方'},\n",
       "  {'words': '纳税人识别号：622102712730896'},\n",
       "  {'words': '地址、电话：广东省深圳市益田路706号0755-244546845'},\n",
       "  {'words': '开户行及账号：中国工商银行深圳支行6220142000104033xx'},\n",
       "  {'words': '注'},\n",
       "  {'words': '源机快递公'},\n",
       "  {'words': '收款人：关一一'},\n",
       "  {'words': '复核：郑一一'},\n",
       "  {'words': '开票人：王建华'},\n",
       "  {'words': '销售：（拿0117940983220Y'},\n",
       "  {'words': '发票专用章'}],\n",
       " 'words_result_num': 91,\n",
       " 'log_id': 1513860843058615964}"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "通用文字识别（高精度版）\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('F:/大三上-API/img/img6.png', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = '24.76e30f9165844c9d19d3480d1f74a5ff.2592000.1652357925.282335-25954484'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    display (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
